% find scaling factor to minimize deviations
% hyzhou, 11/22/2017
% Inputs:
%   x_sim: location of simulation points, 1D row vector
%   T_sim: simulated temperatures
%   x_exp: location of experimental data relative to heating point, can
%   have 1 row or 2 rows. For now we assume isotropic heating, so only one
%   row is supported.
%   T_exp: experimental results
%   T_exp_err: error bars on experimental results
% Outputs:
%   beta: scaling factor that minimizes weighted least square errors

function [beta, varargout] = fit_scaling_factor(x_sim, T_sim, x_exp, T_exp, T_exp_err)

if min(size(x_exp)) == 1
    T_sim_c = interp1(x_sim, T_sim, x_exp);
    [xData, yData, weights] = prepareCurveData( T_sim_c, T_exp, 1./T_exp_err.^2 );
    ft = fittype( 'a*x', 'independent', 'x', 'dependent', 'y' );
    opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
    opts.Display = 'Off';
    opts.Weights = weights;
    [fitresult, gof] = fit( xData, yData, ft, opts );
    beta = fitresult.a;
    varargout{1} = diff(confint(fitresult),1);
    varargout{2} = gof;
else
    error('Not supported!')
end